//index.js
//获取应用实例
const app = getApp()

let httpsUrl = app.httpsPrefix;

const {$Message} = require('../../dist/base/index');
const {$Toast} = require('../../dist/base/index');

Page({
    data: {
        picUrl: "", showView: false
    },

    onLoad: function (options) {
        $Toast({
            mask: false,
            content: '生成签名货单...',
            type: 'loading'
        });

        let base64 = options.base
        //获取上个界面传递过来的合成后的签单的url

        this.setData({ imgurl: "data:image/png;base64," + base64});

        $Toast.hide()
        this.setData({
            signed: options.signed,
            showView: true
        })
    },
    navToSign: function () {
        wx.navigateTo({
            url: '../handWriting/handWriting'
        })
    }, showPreview: function () {
        wx.previewImage({
            current: this.imgUrl, // 当前显示图片的http链接
            urls: [this.imgUrl] // 需要预览的图片http链接列表
        })
    },
    uploadPdf: function () {

        wx.showModal({
            title: "提示",
            showCancel: true,
            content: "您确认上传签名单据到服务器么？",
            success: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '正在上传..',
                    })
                    this.confirmUpload();
                }
            }
        })
    },
    confirmUpload: function () {
        let order = wx.getStorageSync("order")
        let account = wx.getStorageSync("username")

        console.log(this.data.picUrl)

        let filename = this.data.signed

        wx.request({
            url: httpsUrl,
            data: {
                account:account,
                query: 'mp_uploadhecheng',
                单号: order,
                compress_name: filename
            },
            success: ({data}) => {
                wx.hideLoading();
                wx.showModal({
                    title: "提示",
                    showCancel: false,
                    content: data.message,
                    //点击确认返回main界面
                    success: function (res) {
                        if (res.confirm) {
                            wx.navigateBack({
                                delta: 4
                            })
                        }
                    }
                })
            },
            fail: function (res) {
                wx.hideLoading()
                wx.showModal({
                    title: "提示",
                    showCancel: false,
                    content: "网络错误，请稍后重试",
                    success: function (res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                })
            }
        })
    }
})
